import React, { PureComponent } from 'react';
import { connect } from 'dva';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import BatchUpload from '../../components/BatchUpload/Index';
import FileUpload from '../../components/BatchUpload/Upload';
import Finish from '../../components/BatchUpload/Finish';

@connect(({ adjust, publicdata, loading }) => ({
  uploading: loading.effects['adjust/uploadFileWithProgress'],
  fileId: publicdata.downloadFile.fileId,
  fileName: publicdata.downloadFile.fileName,
  currentStep: adjust.currentStep,
  progressPercent: adjust.progressPercent,
  resultInfo: adjust.resultInfo,
  showProgress: adjust.showProgress,
}))
class Batch extends PureComponent {
  componentDidMount() {
    this.clearModel();
  }
  
  componentWillUnmount() {
    this.clearModel();
  }

  clearModel = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'adjust/updateModelData',
      payload: {
        currentStep: 0,
        progressPercent: 0,
        showProgress: false,
      },
    });
  };

  // 设置当前位置
  setCurrentstep = currentStep => {
    const { dispatch } = this.props;
    dispatch({
      type: 'adjust/updateModelData',
      payload: { currentStep },
    });
    if (!currentStep) {
      this.clearModel();
    }
  };

  // 上传文件：需要携带参数即传：batchQuery
  uploadFile = formData => {
    const { dispatch, resultInfo } = this.props;
    dispatch({
      type: 'adjust/uploadFileWithProgress',
      payload: { formData, type: 'batch' },
    }).then(res => {
      if (res.success) {
        dispatch({
          type: 'adjust/updateModelData',
          payload: {
            showProgress: true,
            resultInfo: {
              ...resultInfo,
              errorUrl: '/osApi/insEntryAdj/exportExcelErrData', // 下载错误信息地址
              errorParams: null, // 下载错误数据参数
            },
            // resultInfo: {
            //   // 结果数据必传
            //   batchName: '批量申报结果导入', // 任务名称
            //   taskCount: 0, // 导入数据
            //   successCount: 0, // 成功数据
            //   errorCount: 0, // 失败数据
            //   batchNo: '', // 批次号
            //   errorUrl: '/osApi/insEntryAdj/exportExcelErrData', // 下载错误信息地址
            //   errorParams: null, // 下载错误数据参数
            // },
          },
        });
      }
    });
  };

  render() {
    const { uploading, currentStep, showProgress, progressPercent, resultInfo } = this.props;
    return (
      <PageHeaderWrapper>
        <BatchUpload currentStep={currentStep}>
          <FileUpload
            title="上传文件"
            currentStep={currentStep}
            setCurrentstep={this.setCurrentstep}
            downloadTempInfo={{
              // 下载模板信息
              params: {},
              url: '/osApi/insEntryAdj/downloadTemplate',
              type: 'POST',
            }}
            uploadFile={this.uploadFile}
            uploading={uploading}
            // 展示进度条需要的数据
            progressPercent={progressPercent || 0}
            progressTitle="批量申报结果导入"
            progressVisible={showProgress}
          />
          <Finish title="完成" resultInfo={resultInfo} setCurrentstep={this.setCurrentstep} />
        </BatchUpload>
      </PageHeaderWrapper>
    );
  }
}
export default Batch;
